package verify

import (
	"github.com/hwcer/cosgo"
	"github.com/hwcer/updater"
	"github.com/hwcer/yyds/players/verify"
	"server/game/model"
	"server/share/config"
)

func init() {
	cosgo.On(cosgo.EventTypLoaded, func() error {
		taskSubmit := &TaskSubmit{}
		taskSubmitExpire := &TaskSubmit{Expire: true}
		Register(config.Data.Functions.TaskSubmit, taskSubmit)
		Register(config.Data.Functions.TaskSubmitExpire, taskSubmitExpire)
		return nil
	})
}

// 任务完成次数
type TaskSubmit struct {
	Expire bool
}

func (this *TaskSubmit) Value(u *updater.Updater, value verify.Value) (r int64) {
	coll := u.Collection(config.ITypeTask)
	now := u.Unix()
	for _, k := range value.GetArgs() {
		if k == 0 {
			continue
		}
		if i := coll.Get(k); i != nil {
			v := i.(*model.Task)
			if v.Submit > 0 && (this.Expire || !v.MayRefresh(now)) {
				r += 1
			}
		}
	}
	return
}

func (this *TaskSubmit) Target(u *updater.Updater, value verify.Value) {
	coll := u.Collection(config.ITypeTask)
	for _, k := range value.GetArgs() {
		if k > 0 {
			coll.Select(k)
		}
	}
}
